package permission

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"private-domain-overseas-service/admin/business/api/internal/svc"
	"private-domain-overseas-service/admin/business/api/internal/types"
	"private-domain-overseas-service/pkg/common"
	"private-domain-overseas-service/pkg/status/bizadmin"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/adminuser"
	"private-domain-overseas-service/tools/ent/predicate"
	"time"
)

type AdminUserDao struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAdminUserDao(ctx context.Context, svcCtx *svc.ServiceContext) *AdminUserDao {
	return &AdminUserDao{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// AdminUserCreate - 创建后台用户
func (l *AdminUserDao) AdminUserCreate(adminUser *ent.AdminUser, tx *ent.Tx) (*ent.AdminUser, error) {
	ormClient := l.svcCtx.Orm.AdminUser
	if tx != nil {
		ormClient = tx.AdminUser
	}

	adminUser, err := ormClient.Create().
		SetAdminUserUsername(adminUser.AdminUserUsername).
		SetAdminUserPassword(adminUser.AdminUserPassword).
		SetAdminUserStatus(adminUser.AdminUserStatus).
		SetAdminUserEmail(adminUser.AdminUserEmail).
		SetAdminUserNickname(adminUser.AdminUserNickname).
		Save(l.ctx)
	if err != nil {
		return nil, err
	}
	return adminUser, nil
}

// AdminUserUpdate - 更新后台用户
func (l *AdminUserDao) AdminUserUpdate(updateAdminUser *ent.AdminUser, tx *ent.Tx) error {
	ormClient := l.svcCtx.Orm.AdminUser
	if tx != nil {
		ormClient = tx.AdminUser
	}

	err := ormClient.UpdateOneID(updateAdminUser.ID).
		SetAdminUserUsername(updateAdminUser.AdminUserUsername).
		SetAdminUserPassword(updateAdminUser.AdminUserPassword).
		SetAdminUserStatus(updateAdminUser.AdminUserStatus).
		SetAdminUserEmail(updateAdminUser.AdminUserEmail).
		SetAdminUserNickname(updateAdminUser.AdminUserNickname).
		SetUpdateAt(time.Now()).
		SetIsDelete(updateAdminUser.IsDelete).
		Exec(l.ctx)
	if err != nil {
		return err
	}
	return nil
}

// AdminUserDelete - 删除后台用户
func (l *AdminUserDao) AdminUserDelete(conditions []predicate.AdminUser) error {
	err := l.svcCtx.Orm.AdminUser.Update().
		Where(conditions...).
		SetIsDelete(bizadmin.Delete).
		SetDeleteAt(time.Now()).
		Exec(l.ctx)
	if err != nil {
		return err
	}
	return nil
}

// AdminUserConditionsPageAll - 分页查询后台用户列表 支持链接名，备注 相似查询
func (l *AdminUserDao) AdminUserConditionsPageAll(req *types.AdminUserListRequest, conditions []predicate.AdminUser) ([]*ent.AdminUser, int, error) {
	offset := common.Offset(req.Current, req.PageSize)

	adminUsers, err := l.svcCtx.Orm.AdminUser.Query().
		Where(conditions...).
		Where(adminuser.IsDeleteEQ(bizadmin.NotDelete)).
		Offset(int(offset)).
		Limit(int(req.PageSize)).
		All(l.ctx)

	if err != nil {
		return nil, 0, err
	}

	total, err := l.svcCtx.Orm.AdminUser.Query().
		Where(conditions...).
		Where(adminuser.IsDeleteEQ(bizadmin.NotDelete)).
		Count(l.ctx)

	return adminUsers, total, nil
}

// AdminUserFirst - 根据条件查询后台用户
func (l *AdminUserDao) AdminUserFirst(conditions []predicate.AdminUser) (*ent.AdminUser, error) {
	adminUser, err := l.svcCtx.Orm.AdminUser.Query().
		Where(conditions...).
		First(l.ctx)

	if err != nil {
		return nil, err
	}
	return adminUser, nil
}
